/**
 *
 * ......................我佛慈悲.......................
 *                       _oo0oo_
 *                      o8888888o
 *                      88" . "88
 *                      (| -_- |)
 *                      0\  =  /0
 *                    ___/`---'\___
 *                  .' \\|     |// '.
 *                 / \\|||  :  |||// \
 *                / _||||| -卍-|||||- \
 *               |   | \\\  -  /// |   |
 *               | \_|  ''\---/''  |_/ |
 *               \  .-\__  '-'  ___/-. /
 *             ___'. .'  /--.--\  `. .'___
 *          ."" '<  `.___\_<|>_/___.' >' "".
 *         | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *         \  \ `_.   \_ __\ /__ _/   .-` /  /
 *     =====`-.____`.___ \_____/___.-`___.-'=====
 *                       `=---='
 *
 *..................佛祖开光 ,永无BUG....................
 *         CSDN博客:http://blog.csdn.net/song5347
 *       github地址:https://github.com/lingsuiderongyan
 *             邮箱:lingsuiderongyan@163.com
 */
//自定义发微博toolbar
import UIKit

//MARK: - toolBar按钮的枚举类型
enum LSComposeToolBarButtonType: Int {
    //图片
    case Picture = 0
    //@
    case Mention = 1
    //#
    case Trend = 2
    //表情
    case Emoticon = 3
    //加号
    case Add = 4
}

class LSComposeToolBar: UIStackView {
    
    //MARK: - 点击按钮需要执行的闭包
    var didSelecteToolBarButtonClosuer: ((type: LSComposeToolBarButtonType)->())?
    
    //表情按钮
    var emoticonButton: UIButton?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 添加控件设置布局方式
    private func setupUI() {
        //水平布局
        axis = .Horizontal
        //子控件等比填充
        distribution = .FillEqually
        
        addChildButton("compose_toolbar_picture", type: .Picture)
        addChildButton("compose_mentionbutton_background", type: .Mention)
        addChildButton("compose_trendbutton_background", type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background", type: .Emoticon)
        addChildButton("compose_add_background", type: .Add)
    }
    
    //MARK: - 创建子按钮的通用方法
    private func addChildButton(imageName: String, type: LSComposeToolBarButtonType) -> UIButton {
        let button = UIButton()
        
        //根据枚举的原始值,作为按钮的tag
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: "buttonAction:", forControlEvents: .TouchUpInside)
        
        //设置图片
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        
        //设置背景图片
        //button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        //取消高亮效果
        //button.adjustsImageWhenHighlighted = false
        button.backgroundColor = UIColor(patternImage: UIImage(named: "compose_toolbar_background")!)
        
        //添加按钮
        addArrangedSubview(button)
        
        return button
    }
    
    //MARK: - 点击事件
    @objc private func buttonAction(button: UIButton) {
        
        //通过原始值获取枚举
        let type = LSComposeToolBarButtonType(rawValue: button.tag)!
        
        //执行闭包,传入参数(枚举)
        didSelecteToolBarButtonClosuer?(type: type)
    }
    
    //MARK: - 根据外界传入的键盘类型设置不同的icon
    func switchEmotionIcon(isEmoticon: Bool) {
        //如果外界告诉我们是表情键盘,那么显示系统键盘的icon,否则显示表情的icon
        if isEmoticon {
            //  显示系统键盘的icon
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        } else {
            //  显示表情键盘的icon
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoticonButton?.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
}



















